package org.alive.ee.app.service;

import java.util.List;

import org.alive.ee.app.repository.BaseDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
public class BaseService {

	@Autowired
	protected BaseDAO baseDAO;

	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	public <E> void save(E entity) {
		baseDAO.save(entity);
	}

	public <E> boolean update(E entity) {
		return baseDAO.update(entity);
	}

	public <E> boolean delete(E entity) {
		return baseDAO.delete(entity);
	}
	
	/**
	 * delete entity by class and id
	 * 
	 * @param clz
	 * @param id
	 * @return
	 */
	public <E, K> boolean delete(Class<E> clz, K id) {
		return baseDAO.delete(baseDAO.find(clz, id));
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public <E, K> E find(Class<E> clz, K id) {
		return baseDAO.find(clz, id);
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public <E> List<E> query(Class<E> clz) {
		return baseDAO.query(clz);
	}
}
